package com.jiazhong.project.servlet;

import com.jiazhong.project.pojo.Brand;
import com.jiazhong.project.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/addBrandServlet")
public class AddBrandServlet extends HttpServlet {
    BrandService brandService = new BrandService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取页面输入信息
        String brandName = req.getParameter("brandName");
        String companyName = req.getParameter("companyName");
        String ordered = req.getParameter("ordered");
        String description = req.getParameter("description");
        String status = String.valueOf(req.getParameter("status") != null ? 1 : 0);

        //创建对象
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(Integer.valueOf(ordered));
        brand.setDescription(description);
        brand.setStatus(Integer.valueOf(status));

        System.out.println(brand);

        brandService.insertAll(brand);
     //提交事务

        // 重定向到查询所有品牌的页面
        req.getRequestDispatcher("/brandServlet").forward(req,resp);

    }
}
